// 确保只有一处类定义
#ifndef CONFIG_H
#define CONFIG_H
#include "header.cpp"
#include "files.cpp"

class Config
// 父类（基类）
{
private:
  JSONVar wifiConf;
  JSONVar mqttConf;
  Files files;
  String file_name="/config.json";
public:
  void defaultConf(){
        defaultConfWifi();
        defaultConfMqtt();
  }
  // WIFI 默认配置 账号密码
  void defaultConfWifi(){
      JSONVar conf;
       conf["ssid"] = "hy1";
       conf["pwd"] = "13363641456";
       wifiConf=conf;
        // save();
        // wifiConf["ip"] = "13363641456";
  }
  // mqtt 默认配置
  void defaultConfMqtt(){
    JSONVar conf;
      conf["mqtt_port"] = 8883;
      // 去 EMQX 官网注册账号可以申请免费额度
      conf["mqtt_broker"] = "n9a7eff0.ala.cn-hangzhou.emqxsl.cn";
      // 订阅主题
      conf["mqtt_subscribe_topic"] = "emqx/esp8266";
      // 发布主题
      conf["mqtt_publish_topic"] = "emqx/esp8266";
      // 用户名
      conf["mqtt_username"] = "test";
      // 密码
      conf["mqtt_password"] = "test123456";
    //NTPServer 是一个开源的网络时间协议 还没测 NTP Server settings
      conf["ntp_server"] = "pool.ntp.org";// Recommended NTP server for users in China
      conf["gmt_offset_sec"] = 0; // GMT offset in seconds (adjust for your time zone)
      conf["daylight_offset_sec"] = 0;// Daylight saving time offset in seconds
      // 每间隔多少秒发布一次当前状态
      conf["push_state_m"] = 20;
      // SSL certificate for MQTT broker
    // Load DigiCert Global Root G2, which is used by EMQX Public Broker: broker.emqx.io
    // static const char ca_cert[]
    // PROGMEM = R"EOF()EOF";
      // 服务端的 ssl/tls ca证书
      conf["ca_cert"] = R"EOF(
    -----BEGIN CERTIFICATE-----
    MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh
    MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
    d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9v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v1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=
    -----END CERTIFICATE-----
    )EOF";
   mqttConf= conf;
    // save();
  }
  Config(){
    JSONVar json=files.readJson(file_name);
    if(json){
      if(json["wifiConf"]){
        wifiConf=json["wifiConf"];
      }else{
         defaultConfWifi();
      }
      if(json["mqttConf"] &&  json["mqttConf"]["push_state_m"] && json["mqttConf"]["mqtt_broker"] && json["mqttConf"]["mqtt_subscribe_topic"] &&  json["mqttConf"]["mqtt_port"] &&  json["mqttConf"]["mqtt_publish_topic"]){
        mqttConf=json["mqttConf"];
      }else{
        defaultConfMqtt();
      }
    }else {
      defaultConf();

    }
      //  String jsonString = JSON.stringify(json);
      //  Serial.println(jsonString);  //打印esp8266的IP地址
  }
  bool save(){
    JSONVar json;
    if(wifiConf){
    json["wifiConf"]=wifiConf;
    }
    if(mqttConf){
    json["mqttConf"]=mqttConf;
    }
    if(json){
      return files.writeJson(json,file_name);
    }
    return false;
  }
  bool setWifiConf(JSONVar conf){
      wifiConf=conf;
      return save();
  }
  bool setMqttConf(JSONVar conf){
      mqttConf=conf;
      return save();
  }
  JSONVar getWifiConf(){
    if(wifiConf){
        return wifiConf;
    }
    defaultConfWifi();
      return wifiConf;
  }
  JSONVar getMqttConf(){
    if(mqttConf){
        return mqttConf;
    }
     defaultConfMqtt();
    return mqttConf;
  }

};
#endif
